﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gma.QrCodeNet.Encoding;

namespace QrcodeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write(@"Type some text to QR code: ");
            string sampleText = Console.ReadLine();//读入数据
            bool flag = true;//作为循环开始的判断

            while(flag)
            {
                if(sampleText.Length > 8)//长度判断
                {
                    Console.WriteLine("Your test is too long; please rewrite!");//不符提示
                    Console.Write(@"Type some text to QR code: ");
                    sampleText = Console.ReadLine();
                    flag = true;
                }
                else
                {
                    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);//设置二维码纠错等级
                    QrCode qrCode = qrEncoder.Encode(sampleText);//传入输入的数据
                    for (int j = 0; j < qrCode.Matrix.Width; j++)//绘制二维码
                    {
                        for (int i = 0; i < qrCode.Matrix.Width; i++)
                        {

                            char charToPrint = qrCode.Matrix[i, j] ? '█' : ' ';
                            Console.Write(charToPrint);
                        }
                        Console.WriteLine();//换行
                    }
                    Console.WriteLine(@"Press any key to quit.");
                    flag = false;
                }
            }
            
            Console.ReadKey();//等待键盘输入，退出程序
        }
    }
}
